package com.txunda.zhuohired.aty.user.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.my.ApplyShareAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.my.ApplyShareBean;
import com.txunda.zhuohired.http.pst.InfoPst;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/27 10:57
 * 功能描述：求职分享
 * 联系方式：jiahuanchen@126.com
 */

public class ApplyShareAty extends BaseAty {
    /**
     * 求职分享
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * listView
     */
    @ViewInject(R.id.lv_apply_share)
    private PullToRefreshListView lv_apply_share;
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private ApplyShareAdapter applyShareAdapter;
    private String token;
    private InfoPst infoPst;
    private int page = 1;
    private List<ApplyShareBean.DataBean> applyShareList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("求职分享");

        lv_apply_share.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initHttp();
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_apply_share;
    }

    @Override
    protected void initialized() {
        infoPst = new InfoPst(this);
        token = application.getUserInfo().get("data");
    }

    @Override
    protected void requestData() {
        initHttp();
//        initAdapter();
    }
    private void initHttp(){
        infoPst.shareJob(token,String.valueOf(page));
    }

    private void initAdapter() {
        applyShareAdapter = new ApplyShareAdapter(this,applyShareList);
        lv_apply_share.setAdapter(applyShareAdapter);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if(requestUrl.contains("shareJob")){
//            L.e("=====shareJob=====",jsonStr);
            lv_apply_share.onRefreshComplete();
            ApplyShareBean applyShareBean = GsonUtil.GsonToBean(jsonStr,ApplyShareBean.class);

            applyShareList = applyShareBean.getData();

            if(page==1){
                if(applyShareList.size()==0){
                    noData(2);
                }else{
                    noData(1);
                    initAdapter();
                }
            }else{
                if(applyShareBean.getData().size()==0){
                    showToast("已经最后一页了，没有数据了");
                }else{
                    List myEventList = applyShareBean.getData();
                    applyShareList.addAll(myEventList);
                    applyShareAdapter.notifyDataSetChanged();
                }
            }
            initAdapter();
        }
    }

    /**
     * 空数据
     *
     * @param type
     */
    private void noData(int type){
        if(type==1){
            lv_apply_share.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        }else{
            lv_apply_share.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }
}
